package com.dao;

import com.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.tool.Connecttool;

import java.sql.SQLException;
import java.util.List;
@Repository
public class AccountdaoImpl implements Accountdao {
    @Autowired
    private QueryRunner runner;
    @Autowired
    private Connecttool connecttool;

    public void setConnecttool(Connecttool connecttool) {
        this.connecttool = connecttool;
    }

    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }

    @Override
    public List<Account> saveAccount() {
        try {
            return runner.query(connecttool.gettreadconnect(),
                    "select * from account",
                    new BeanListHandler<Account>(Account.class));
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }

    @Override
    public Account findbyid(Integer id) {
        try {
            return runner.query(connecttool.gettreadconnect(),"select * from account where id= ?",
                    new BeanHandler<Account>(Account.class),id);
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }

    @Override
    public int update(Account account) {
        try {
            return runner.update(connecttool.gettreadconnect(),"update account set name = ?,money = ? where id = ?",
                    account.getName(),account.getMoney(),account.getId());
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }

    @Override
    public int delete(Integer id) {
        try {
            return runner.update(connecttool.gettreadconnect(),"delete\n" +
                            "from limanspring.account\n" +
                            "where id = ?",id);
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }

    @Override
    public int sace(Account account) {
        try {
            return runner.update(connecttool.gettreadconnect(),"insert into limanspring.account\n" +
                            "             (name,\n" +
                            "             money)\n" +
                            "values (?,?)",account.getName(),account.getMoney());
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }

    @Override
    public Account transfaer(String name) {
        try {
            List<Account>list=runner.query(connecttool.gettreadconnect(),"select * from account " +
                            "where name=?",
                    new BeanListHandler<Account>(Account.class),name);
            if (list==null||list.size()==0)return null;
            else if (list.size()>1)throw new RuntimeException(
                    "结果不为1！");
            else return list.get(0);
        } catch (SQLException throwables) {
            throw new RuntimeException("你好");
        }
    }
}
